package com.thomshutt.fappybird; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.thomshutt.fappybird.drawable.Background; import com.thomshutt.fappybird.drawable.Bird; import com.thomshutt.fappybird.drawable.PipeFactory; import java.util.ArrayList; import java.util.List; public class FappyBird implements ApplicationListener { private static final String IMAGE_BIRD_1 = "data/bird_1.png"; private static final String IMAGE_BIRD_2 = "data/bird_2.png"; private static final String IMAGE_BIRD_3 = "data/bird_3.png"; private static final String IMAGE_BACKGROUND = "data/background.png"; private static final String IMAGE_FLOOR = "data/floor.png"; private static final String IMAGE_PIPE = "data/pipe.png"; private static final String IMAGE_PIPE_TOP = "data/pipe_top.png"; private static final int SCROLL_SPEED_FLOOR = 280; private static final int SCROLL_SPEED_BACKGROUND = 80; private Bird bird; private Background floor; private int width; private int height; private PipeFactory pipeFactory; private BitmapFont FONT_WHITE; private BitmapFont FONT_BLACK; private static enum STATES {RUNNING, LOST, DEATH_THROES}; private OrthographicCamera camera; private SpriteBatch batch; private List<Drawable> drawables; private STATES state = STATES.RUNNING; @Override public void create() { FONT_WHITE = new BitmapFont(Gdx.files.internal("data/font_white.fnt"), Gdx.files.internal("data/font_white.png"), false); FONT_BLACK = new BitmapFont(Gdx.files.internal("data/font.fnt"), Gdx.files.internal("data/font.png"), false); FONT_BLACK.setScale(2.5f); FONT_WHITE.setScale(2.5f); Animation birdAnimation = new Animation( 0.3f, new TextureRegion(new Texture(Gdx.files.internal(IMAGE_BIRD_1))), new TextureRegion(new Texture(Gdx.files.internal(IMAGE_BIRD_2))), new TextureRegion(new Texture(Gdx.files.internal(IMAGE_BIRD_3))), new TextureRegion(new Texture(Gdx.files.internal(IMAGE_BIRD_2))) ); birdAnimation.setPlayMode(Animation.LOOP); this.batch = new SpriteBatch(); this.drawables = new ArrayList<Drawable>(); this.pipeFactory = new PipeFactory(Gdx.files.internal(IMAGE_PIPE), Gdx.files.internal(IMAGE_PIPE_TOP), SCROLL_SPEED_FLOOR); this.bird = new Bird(birdAnimation); this.floor = new Background(Gdx.files.internal(IMAGE_FLOOR), SCROLL_SPEED_FLOOR); drawables.add(new Background(Gdx.files.internal(IMAGE_BACKGROUND), SCROLL_SPEED_BACKGROUND)); drawables.add(pipeFactory); drawables.add(bird); drawables.add(floor); } @Override public void dispose() { batch.dispose(); } @Override public void render() { doUpdate(); doRender(); } private void doUpdate(){ if(this.state == STATES.LOST){ if(inputTouched()){ restartGame(); } return; } if(this.state == STATES.DEATH_THROES){ bird.tick(Gdx.graphics.getDeltaTime()); } else { for (Drawable drawable : drawables) { drawable.tick(Gdx.graphics.getDeltaTime()); drawable.screenTouched(inputTouched()); if(drawable.isInCollisionWith(bird)){ this.state = STATES.DEATH_THROES; } } } if(floor.isInCollisionWith(bird)){ this.state = STATES.LOST; } } private void doRender(){ Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); batch.setProjectionMatrix(camera.combined); batch.begin(); for (Drawable drawable : drawables) { drawable.draw(batch); } BitmapFont.TextBounds bounds = FONT_BLACK.getBounds(String.valueOf(this.pipeFactory.getNumPipesPassed())); FONT_WHITE.draw(batch, String.valueOf(this.pipeFactory.getNumPipesPassed()), -(bounds.width / 2), (bounds.height / 2) + (this.height / 3)); FONT_BLACK.draw(batch, String.valueOf(this.pipeFactory.getNumPipesPassed()), -(bounds.width / 2) + 3, (bounds.height / 2) + (this.height / 3) - 3); batch.end(); ShapeRenderer shapeRenderer = new ShapeRenderer(); shapeRenderer.setProjectionMatrix(camera.combined); shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); shapeRenderer.setColor(1, 1, 0, 1); // shapeRenderer.circle(bird.getCircle().x, bird.getCircle().y, bird.getCircle().radius); shapeRenderer.end(); } @Override public void resize(int width, int height) { this.width = width; this.height = height; camera = new OrthographicCamera(width, height); for (Drawable drawable : drawables) { drawable.resize(width, height); } } @Override public void pause() {} @Override public void resume() {} private void restartGame(){ this.create(); this.resize(this.width, this.height); this.state = STATES.RUNNING; this.bird.screenTouched(true); } private boolean inputTouched(){ return Gdx.input.justTouched() || Gdx.input.isKeyPressed(Input.Keys.SPACE) || Gdx.input.isKeyPressed(Input.Keys.UP); } }